package com.itheima.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.LocalDateTimeConverter;
import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 分类分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> getPage(Integer page,Integer pageSize){
        log.info("当前页数："+page+"数据"+pageSize);
        //构建分页构造器
        Page pageIn = new Page(page,pageSize);
        //构建条件构造器
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        categoryService.page(pageIn,wrapper);
        return R.success(pageIn);
    }

    /**
     * 添加菜品、套餐
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("添加新员工");
        categoryService.save(category);
        return R.success("添加成功");
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id){
        log.info("删除所选id");
//        categoryService.removeById(id);
        categoryService.remove(id);
        return R.success("删除成功");
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping
    public R<String> delete(@RequestBody Category category){
        log.info("修改当前id");
        categoryService.updateById(category);
        return R.success("修改成功");
    }
    /**
     * 根据条件查询分类数据
     * @param category
     * @return
     */
    @GetMapping("list")
    public R<List<Category>> list(Category category){
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }
//    @GetMapping("exportExcel")
//    public void exportExcel(HttpServletResponse response) throws IOException {
//        //1、设定响应类型
//        response.setContentType("application/vnd.ms-excel");
//        //2、设定附件的打开方法为：下载，并指定文件名称为category.xlsx
//        response.setHeader("content-disposition", "attachment;filename=category.xlsx");
//        //3、、创建工作簿
//        ExcelWriterBuilder writeWork = EasyExcel.write(response.getOutputStream(), Category.class).registerConverter(new LocalDateTimeConverter());
//        //4、创建表格
//        ExcelWriterSheetBuilder sheet = writeWork.sheet();
//        //5、调用业务层获取数据
//        List<Category> categories = categoryService.list();
//        //6、写入数据到表格中
//        sheet.doWrite(categories);
//    }
}
